﻿using CTM.Web.Models.Input;
using CTM.Web.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace CTM.Web.Models.View.Feedback
{
    public class FeedbackViewModel
    {
        #region Properties

        public Guid Id { get; set; }

        [Required(ErrorMessage = "*")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [Required(ErrorMessage = "*")]
        public Emoticon Emoticon { get; set; }

        public bool IsPrivate { get; set; }

        public bool IsDeleted { get; set; }

        public DateTime CreatedOn { get; set; }

        [StringLength(128)]
        public string CreatedBy { get; set; }

        public DateTime? ModifiedOn { get; set; }

        [StringLength(128)]
        public string ModifiedBy { get; set; }

        #endregion

        #region Relationship

        public virtual Category Category { get; set; }
        public virtual ICollection<Input.Comment> Comments { get; set; }
        public virtual ICollection<Tag> Tags { get; set; }

        // giver
        public virtual ApplicationUser Manager { get; set; }

        public virtual ApplicationUser Employee { get; set; }

        #endregion

        public FeedbackViewModel()
        {
            Emoticon = Emoticon.Neutral;
            IsDeleted = false;
            CreatedOn = DateTime.UtcNow;
            Comments = new HashSet<Input.Comment>();
            Tags = new HashSet<Tag>();
        }

        public FeedbackViewModel(Input.Post feedback)
        {
            this.Id = feedback.Id;
            this.Description = feedback.Description;
            this.Emoticon = feedback.Emoticon.Value;
            this.IsPrivate = feedback.IsPrivate;
            this.IsDeleted = feedback.IsDeleted;
            this.CreatedOn = ApplicationHelpers.ConvertToLocalTime(feedback.CreatedOn);
            this.CreatedBy = feedback.CreatedBy;
            this.ModifiedOn = feedback.ModifiedOn.HasValue ? ApplicationHelpers.ConvertToLocalTime(feedback.ModifiedOn.Value) : feedback.ModifiedOn;
            this.ModifiedBy = feedback.ModifiedBy;
            this.Category = feedback.Category;
            this.Comments = feedback.Comments;
            this.Tags = feedback.Tags;
            this.Manager = feedback.Manager;
            this.Employee = feedback.Employee;
        }
    }

    public class FeedbackUpdateModel
    {
        public Guid Id { get; set; }
        [Required]
        public Emoticon Emoticon { get; set; }
        [Required]
        public string Description { get; set; }
        public List<string> Tags { get; set; }
        public bool IsPrivate { get; set; }

        public FeedbackUpdateModel()
        {
            Tags = new List<string>();
        }
    }

    public class FeedbackSearchModel
    {
        public string Id { get; set; }
        public int Year { get; set; }
        public List<Emoticon> Emoticons { get; set; }
        public bool GivenToMe { get; set; }
        public bool GivenByMe { get; set; }
        public string Keyword { get; set; }
        public bool SelectedPageOnly { get; set; }
        public List<string> Tags { get; set; }
        public IEnumerable<FeedbackViewModel> Feedbacks { get; set; }
        public List<Guid> Orgs { get; set; }

        public FeedbackSearchModel()
        {
            Year = DateTime.Today.Year;
            Emoticons = new List<Emoticon>();
            Tags = new List<string>();

            GivenByMe = true;
            GivenToMe = true;
            SelectedPageOnly = true;

            Feedbacks = Enumerable.Empty<FeedbackViewModel>();
        }
    }
}